package com.apps.globals.foot.service.impl;

import com.apps.globals.foot.model.po.KupaFooterCat;
import com.apps.globals.foot.model.po.KupaFooterColumn;
import com.apps.globals.foot.dao.KupaFooterColumnMapper;
import com.apps.globals.foot.service.IKupaFooterCatService;
import com.apps.globals.foot.service.IKupaFooterColumnService;
import com.apps.goods.model.po.KupaGoodsCat;
import com.apps.goods.model.vo.KupaGoodsCatVo;
import com.apps.sannotation.service.OnRedisCache;
import com.apps.sannotation.service.enums.CacheModel;
import com.apps.sworld.dao.impl.sDaoImpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大鹏
 * @since 2020-10-02
 */
@Slf4j
@OnRedisCache(model = CacheModel.ALL)
@Service
public class KupaFooterColumnServiceImpl extends sDaoImpl<KupaFooterColumnMapper, KupaFooterColumn> implements IKupaFooterColumnService
{
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private IKupaFooterCatService footerCatService;

    @Override
    public Map<String, List<KupaFooterColumn>> QueryMap() {
        return (Map<String, List<KupaFooterColumn>>) redisTemplate.opsForValue().get(this.getClass().getSimpleName()+this.currentModelClass().getSimpleName());
    }

    private void QueryAndCacheMasterData()
    {
        List<KupaFooterCat> footerCats=footerCatService.query();
        Map<String, List<KupaFooterColumn>> result=new HashMap<>();
        for(KupaFooterCat cat:footerCats)
            result.put(cat.getTitles(),queryByCatId(cat.getId()));
        redisTemplate.opsForValue().set(this.getClass().getSimpleName()+this.currentModelClass().getSimpleName(),result);
    }

    @Override
    public void onLoad()
    {
        //将所有一级分类加载到缓存
        QueryAndCacheMasterData();
    }

    @Override
    public void onCreate(KupaFooterColumn footerColumn)
    {
        //将所有一级分类加载到缓存
        QueryAndCacheMasterData();
    }

    @Override
    public void onUpdate(KupaFooterColumn footerColumn)
    {
        //将所有一级分类加载到缓存
        QueryAndCacheMasterData();
    }

    @Override
    public void onDelete(List<KupaFooterColumn> footerColumns)
    {
        //将所有一级分类加载到缓存
        QueryAndCacheMasterData();
    }
}
